package BST;

import org.omg.CORBA.NO_IMPLEMENT;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BST<E extends Comparable<E>> {

    private class Node {
        private E e;
        private Node left, right;

        private Node(E e) {
            this.e = e;
        }
    }

    private Node root;
    private int size;

    public BST() {
        root = null;
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public boolean contains(E e){
        if (root == null) {
            System.out.println("BST is empty");
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node res = queue.poll();
            if(res.e == e){
                return true;
            }
            if (res.left != null) {
                queue.add(res.left);
            }
            if (res.right != null) {
                queue.add(res.right);
            }
        }

        return false;
    }

    public void add(E e) {
        if (root == null) {
            size++;
            root = new Node(e);
            return;
        }

        root = add(root, e);
    }

    private Node add(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }
        if (node.e.compareTo(e) < 0) {
            node.right = add(node.right, e);
        } else if (node.e.compareTo(e) > 0) {
            node.left = add(node.left, e);
        }

        return node;
    }

    public void preOrder() {
        if (root == null) {
            System.out.println("BST is null");
            return;
        }
        preOrder(root);
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }

        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    //非递归实现前序遍历
    public void preOrderNR() {
        if (root == null) {
            System.out.println("BST is null");
            return;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node res = stack.pop();
            System.out.println(res.e);
            if (res.right != null) {
                stack.push(res.right);
            }
            if (res.left != null) {
                stack.push(res.left);
            }
        }
    }

    public void inOrder() {
        if (root == null) {
            System.out.println("BST is empty");
            return;
        }
        inOrder(root);
    }

    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    public void postOrder() {
        if (root == null) {
            System.out.println("BST is empty");
            return;
        }
        postOrder(root);
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    public void levelOrder() {
        if (root == null) {
            System.out.println("BST is null");
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node res = queue.poll();
            System.out.println(res.e);
            if (res.left != null) {
                queue.add(res.left);
            }
            if (res.right != null) {
                queue.add(res.right);
            }
        }
    }

    //找出二叉搜索树中的最小值
    public E minmum() {
        if (size == 0) {
            throw new IllegalArgumentException("BST is empty");
        }
        return minmum(root).e;
    }

    private Node minmum(Node node) {
        if (node.left == null) {
            return node;
        }
        return minmum(node.left);

    }

    //找出二叉搜索树中的最大值
    public E maxmum() {
        if (size == 0) {
            throw new IllegalArgumentException("BST is empty");
        }
        return maxmum(root).e;
    }

    private Node maxmum(Node node) {
        if (node.right == null) {
            return node;
        }
        return maxmum(node.right);

    }

    //删除二叉搜索树最小的元素
    public E removeMin() {
        E res = minmum();
        root = removeMin(root);
        size--;
        return res;
    }

    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            return rightNode;
        }

        node.left = removeMin(node.left);
        return node;
    }

    //删除二叉搜索树的最大的元素
    public E removeMax() {
        E res = maxmum();
        root = removeMax(root);
        return res;
    }

    private Node removeMax(Node node) {
        if (node.right == null) {
            Node leftNode = node.left;
            node.left = null;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    //删除二叉搜索树中的元素
    public void remove(E e) {
        if (size == 0) {
            throw new IllegalArgumentException("BST is empty");
        }
        root = remove(root, e);
        size--;
    }

    private Node remove(Node node, E e) {
        if (node == null){
            return null;
        }
        if (node.e.compareTo(e) < 0) {

            node.right =  remove(node.right, e);
            return node;
        } else if (node.e.compareTo(e) > 0) {

            node.left =  remove(node.left, e);
            return node;
        } else {//node.e == e
            //结点的左孩子为空
            if(node.left == null){
                return node.right;
            }
            //结点的右孩子为空
            if(node.right == null){
                return node.left;
            }
            //左右孩子都不为空
            node.e = minmum(node.right).e;
            removeMin(node.right);
            return node;

        }



    }


}






